﻿/// <reference path="scripts/typings/knockout/knockout.d.ts" />
class TaskDetails {
    id: KnockoutObservable<number>;
    title: KnockoutObservable<string>;
    detail: KnockoutObservable<string>;
    editEnabled: KnockoutObservable<boolean>;
    isSelected: KnockoutObservable<boolean>;
    constructor(id: number, title: string, details: string, editEnabled : boolean, isSelected: boolean) {
        this.id = ko.observable(id);
        this.title = ko.observable(title);
        this.detail = ko.observable(details);
        this.editEnabled = ko.observable(editEnabled);
        this.isSelected = ko.observable(isSelected);
    }
}


class TaskViewModel {
    public tasks: KnockoutObservableArray<TaskDetails>;
    constructor() {
        this.tasks = ko.observableArray([]);
    };
    addChild = function () {
        var itemsCount: number;
        itemsCount = this.tasks().length;
        var tasksList = this.tasks();
        //verify whether the last record is populated before entering the next record
        if (tasksList[itemsCount - 1].title() == "") {
            alert("Please enter the title for the last record before creating the next");
        }
        else {
            var maxId: number = 0;
            var nextItemId: number;
            for (var i: number = 0; i < itemsCount; i++) {
                if (tasksList[i].id() > maxId) {
                    maxId = tasksList[i].id();
                }
            }
            nextItemId = maxId + 1;
            this.tasks.push(new TaskDetails(nextItemId, "", "", true, false));
        }
    }
    sortColumns = function () {
        this.tasks.sort(function (taskA, taskB) {
            if (taskA.id() < taskB.id()) {
                return -1;
            }
            else {
                return 1;
            }
        });
    }
    deleteItem = function () {      
        var deletedTaskId: number;
        deletedTaskId = this.id();        
        viewModel.tasks.remove(function (item) {
            return item.id() == deletedTaskId;
        });
    }
    editRow = function () {
        var viewModelTasks = viewModel.tasks();
        var editedRecordId: number;
        editedRecordId = this.id();
        for (var i: number = 0; i < viewModelTasks.length; i++) {
            if (viewModelTasks[i].id() == editedRecordId) {
                viewModelTasks[i].editEnabled(true);
            }
        }
    }

    submitRow = function () {
        var viewModelTasks = viewModel.tasks();
        var editedRecordId: number;
        editedRecordId = this.id();
        //verify that there is some data
        if (this.title() == "") {
            alert("Please enter the title before submitting!");
        }
        else {
            for (var i: number = 0; i < viewModelTasks.length; i++) {
                if (viewModelTasks[i].id() == editedRecordId) {
                    viewModelTasks[i].editEnabled(false);
                }
            }
        }
    }

    saveEditsChanges = function () {
        var viewModelTasks = viewModel.tasks();
        var selectedCount: number = 0;
        for (var i: number = 0; i < viewModelTasks.length; i++) {
            if (viewModelTasks[i].isSelected() == true) {
                selectedCount++;
            }
        }
        alert("You have selected " + selectedCount + " records.");
    }
}

//the JSON data can come from anywhere - hardcoding for this application
var jsonData = [
    { "id": 1, "title": "12 angry men", "detail": "Drama" },
    { "id": 2, "title": "Titanic", "detail": "Romance" },
    { "id": 3, "title": "Schindler's list", "detail": "History" }
]


var serverdata: any[];
var viewModel: TaskViewModel;
viewModel = new TaskViewModel();
var i: number;
for (i = 0; i < jsonData.length; i++) {
    var serverTask: any;
    serverTask = jsonData[i];
    viewModel.tasks.push(new TaskDetails(serverTask.id, serverTask.title, serverTask.detail, false, false));
}
ko.applyBindings(viewModel);